set more off
clear
cd "D:\Projects\HeterogeneousExternalities\Data\"

use Daily_Data.dta,clear

cap shell rmdir "D:\Projects\HeterogeneousExternalities\Data\TrashCan" /s /q   
cap mkdir "D:\Projects\HeterogeneousExternalities\Data\TrashCan"


*Loop over counties
levelsof county_fe
foreach lev in `r(levels)'{

use Daily_Data.dta,clear
display `lev'
keep if county_fe==`lev'


*Ramp regulatory vars
local ramp_length=15

gen RVP9_ramp=RVP9
gen RVP78_ramp=RVP78
gen RVP7_ramp=RVP7
gen RFG_ramp=RFG

sort station_fe date
forvalues i=1/`ramp_length'{
bysort station_fe: replace RVP9_ramp=1-`i'*(1/`ramp_length') if RVP9_on[_n+`i']==1
bysort station_fe: replace RVP9_ramp=1-`i'*(1/`ramp_length') if RVP9_off[_n-`i'+1]==1

bysort station_fe: replace RVP78_ramp=1-`i'*(1/`ramp_length') if RVP78_on[_n+`i']==1
bysort station_fe: replace RVP78_ramp=1-`i'*(1/`ramp_length') if RVP78_off[_n-`i'+1]==1

bysort station_fe: replace RVP7_ramp=1-`i'*(1/`ramp_length') if RVP7_on[_n+`i']==1
bysort station_fe: replace RVP7_ramp=1-`i'*(1/`ramp_length') if RVP7_off[_n-`i'+1]==1

bysort station_fe: replace RFG_ramp=1-`i'*(1/`ramp_length') if RFG_on[_n+`i']==1
bysort station_fe: replace RFG_ramp=1-`i'*(1/`ramp_length') if RFG_off[_n-`i'+1]==1


}

format date %td


gen beta_9=.
gen beta_78=.
gen beta_7=.
gen beta_F=.
gen elasticity=.
gen se_elast=.


	cap ivreghdfe lveh snow depth hdd cdd mean_precip lrGDP  (lrgas=RVP9_ramp RVP78_ramp RVP7_ramp RFG_ramp)  if county_fe==`lev', absorb(DOW MOY  station_fe) cluster(station_fe)
	cap replace elasticity=_b[lrgas] if county_fe==`lev'
	cap replace se_elast=_se[lrgas] if county_fe==`lev'
	
	
	cap reghdfe lrgas RVP9_ramp RVP78_ramp RVP7_ramp RFG_ramp snow depth hdd cdd mean_precip lrGDP  if county_fe==`lev', absorb(DOW MOY  station_fe) cluster(station_fe)
	cap replace beta_9=_b[RVP9_ramp] if county_fe==`lev' & _b[RVP9_ramp]!=0
	cap replace beta_78=_b[RVP78_ramp] if county_fe==`lev' & _b[RVP78_ramp]!=0
	cap replace beta_7=_b[RVP7_ramp] if county_fe==`lev' & _b[RVP7_ramp]!=0
	cap replace beta_F=_b[RFG_ramp] if county_fe==`lev' & _b[RFG_ramp]!=0

save "D:\Projects\HeterogeneousExternalities\Data\TrashCan\\`lev'.dta",replace	


	}
	
	
	

local files2 : dir "D:\Projects\HeterogeneousExternalities\Data\TrashCan" files "*.dta"
cd "D:\Projects\HeterogeneousExternalities\Data\TrashCan"
di `files2'
	
local getfile "use"
local option ", clear"
foreach file in `files2' {
	`getfile' "`file'" `option'
	
	local getfile "append using "
	local option ", force"
	}	
	
cap shell rmdir "D:\Projects\HeterogeneousExternalities\Data\TrashCan" /s /q   
	
cd "D:\Projects\HeterogeneousExternalities\Data\" 
 

sort station_fe date
collapse (first) elasticity veh se_elast  urban_13, by(county_fe county_fips)


save "County_PriceElasticity.dta", replace
use "County_PriceElasticity.dta",clear
drop if elast>2
drop if elas<-2
sum elast if urban==1, d
local large : di %6.2f scalar(`r(mean)')
display `large'
sum elast if urban==2,  d
local fringe : di %6.2f scalar(`r(mean)')
sum elast if urban==3, d
local medium : di %6.2f scalar(`r(mean)')
sum elast if urban==4, d
local small : di %6.2f scalar(`r(mean)')

twoway kdensity elasticity if urban==1, lwidth(medthick) || kdensity elasticity if urban==2,lwidth(medthick) || /*
	*/kdensity elasticity if urban==3,lwidth(medthick) || kdensity elasticity if urban==4,  lpattern(-..*.-) lwidth(thick)/*
	*/xtitle(Point Estimates) ytitle(Density) legend(label(1 "Large Central Metro (`large')")/*
	*/label(2 "Large Fringe Metro (`fringe')") label(3 "Medium Metro (`medium')") label(4 "Small Metro (`small')") pos(11) ) /*
	*/ legend(size(medium))  xlabel(,nogrid) ylabel(,nogrid) name(elast, replace) xline(0, lstyle(p6))


use "County_PriceElasticity.dta",clear
gen t_stat=elast/se_elast	
drop if t_stat>5

sum t_stat if urban==1, d
local large : di %6.2f scalar(`r(mean)')
sum t_stat if urban==2, d
local fringe : di %6.2f scalar(`r(mean)')
sum t_stat if urban==3, d
local medium : di %6.2f scalar(`r(mean)')
sum t_stat if urban==4, d
local small : di %6.2f scalar(`r(mean)')
	
twoway kdensity t_stat if urban==1,lwidth(medthick) || kdensity t_stat if urban==2,lwidth(medthick) || /*
	*/kdensity t_stat if urban==3,lwidth(medthick) || kdensity t_stat if urban==4 ,  lpattern(-.*..-) lwidth(thick)/*
	*/xtitle(Z-Statistics) ytitle(Density) legend(label(1 "Large Central Metro (`large')")/*
	*/label(2 "Large Fringe Metro (`fringe')") label(3 "Medium Metro (`medium')") label(4 "Small Metro (`small')") pos(11) )  legend(size(medium)) /*
	*/ xlabel(-55(10)5,nogrid) ylabel(,nogrid) name(telast,replace) xline(0, lstyle(p6)) 

local m=0
graph combine elast telast ,  altshrink graphregion(color(white) margin(l=`m' r=`m')) col(2) 

	
	
cap shell rmdir "D:\Projects\HeterogeneousExternalities\Data\TrashCan" /s /q   


	
